// #include <bitset>
#include "../tools.h"
#include <bits/stdc++.h>
using namespace std;




class Solution {
public:
    vector<int> findClosedNumbers(int num) {
        bitset<32> small(num);
        bitset<32> bigger(num);

        int s = -1, b = -1;
        // small 10 -> 01， 右侧的 1 移到左侧
        for(int i = 1; i < 31; ++i){
            if(small[i] == 1 && small[i - 1] == 0){
                // cout << i << endl;
                // exit(0);
                small[i] = 0;
                small[i - 1] = 1;
                for(int left = i - 2, right = 0; left > right;){
                    while(left > right && small[left] == 1) --left;
                    while(left > right && small[right] == 0) ++right;
                    // cout << left << " " << right << endl;
                    if(left <= right) break;
                    small[left] = 1;
                    small[right] = 0;
                }
                s = (int) small.to_ulong();
                break;
            }
        }
        // bigger 01 -> 10，右侧的 1 移到右侧
        for(int i = 1; i < 31; ++i){
            if(bigger[i] == 0 && bigger[i - 1] == 1){
                // cout << i << endl;
                // exit(0);
                bigger[i] = 1;
                bigger[i - 1] = 0;
                for(int left = i - 2, right = 0; left > right;){
                    while(left > right && bigger[left]  == 0) --left;
                    while(left > right && bigger[right] == 1) ++right;
                    if(left <= right) break;
                    bigger[left] = 0;
                    bigger[right] = 1;
                }
                b = (int) bigger.to_ulong();
                break;
            }
        }
        return {b, s};
    }
};


int main(int argc, char const *argv[])
{
    Solution sol;
    int num = 2147483647; // 
    printf("%x\n", num);


    auto res = sol.findClosedNumbers(num);
    show(res);





    return 0;
}
